package net.huashitong.taskjob.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.taskjob.entity.TaskJob;
import net.huashitong.taskjob.filter.TaskJobFilter;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class TaskJobDao extends StringDao<TaskJob> {

    /**
     * 任务清单页面查询主入口
     * @param filter
     * @return
     */
    public List<TaskJob> getMissinTask(TaskJobFilter filter)
    {
        GenericQuery query = createQuery( "obj");
        if(StringUtils.isNotBlank(filter.getSysId())){
            query.append(" and sysId =:sysId").setParam("sysId",filter.getSysId());
        }
        if(StringUtils.isNotBlank(filter.getTaskName())){
            query.append(" and taskName like:taskName").setParam("taskName",StringUtils.getLikewords(filter.getTaskName()));
        }
        if(StringUtils.isNotBlank(filter.getTaskContent())){
            query.append(" and taskContent like:taskContent").setParam("taskContent",
                    StringUtils.getLikewords(filter.getTaskContent()));
        }
        if(StringUtils.isNotBlank(filter.getAaa001())){
            query.append(" and aaa001 =:aaa001").setParam("aaa001",filter.getAaa001());
        }
        if(StringUtils.isNotBlank(filter.getTaskTimes())){
            query.append(" and taskTimes =:taskTimes").setParam("taskTimes",filter.getTaskTimes());
        }
        if(StringUtils.isNotBlank(filter.getIndustryName())){
            query.append(" and industryName like:industryName").setParam("industryName",
                    StringUtils.getLikewords(filter.getIndustryName()));
        }
        if(StringUtils.isNotBlank(filter.getIndustryId())){
            query.append(" and industryId =:industryId").setParam("industryId",filter.getIndustryId());
        }
        if(StringUtils.isNotBlank(filter.getPlanStartDate())){
            query.append(" and planStartDate =:planStartDate").setParam("planStartDate",filter.getPlanStartDate());
        }
        if(StringUtils.isNotBlank(filter.getPlanEndDate())){
            query.append(" and planEndDate =:planEndDate").setParam("planEndDate",filter.getPlanEndDate());
        }
        if(StringUtils.isNotBlank(filter.getStartDate())){
            query.append(" and startDate =:startDate").setParam("startDate",filter.getStartDate());
        }
        if(StringUtils.isNotBlank(filter.getEndDate())){
            query.append(" and endDate =:endDate").setParam("endDate",filter.getEndDate());
        }
        if(StringUtils.isNotBlank(filter.getRespResult())){
            query.append(" and respResult =:respResult").setParam("respResult",filter.getRespResult());
        }
        if(StringUtils.isNotBlank(filter.getResDept())){
            query.append(" and resDept like:resDept").setParam("resDept",
                    StringUtils.getLikewords(filter.getResDept()));
        }
        if(StringUtils.isNotBlank(filter.getResUser())){
            query.append(" and resUser like:resUser").setParam("resUser",
                    StringUtils.getLikewords(filter.getResUser()));
        }
        if(StringUtils.isNotBlank(filter.getResPhone())){
            query.append(" and resPhone =:resPhone").setParam("resPhone",filter.getResPhone());
        }
        if(StringUtils.isNotBlank(filter.getSupervisionResult())){
            query.append(" and supervisionResult =:supervisionResult").setParam("supervisionResult",filter.getSupervisionResult());
        }
        if(StringUtils.isNotBlank(filter.getGroupName())){
            query.append(" and groupName like:groupName").setParam("groupName",
                    StringUtils.getLikewords(filter.getGroupName()));
        }
        if(StringUtils.isNotBlank(filter.getInspectors())){
            query.append(" and inspectors like:inspectors").setParam("inspectors",
                    StringUtils.getLikewords(filter.getInspectors()));
        }
        if(StringUtils.isNotBlank(filter.getPhones())){
            query.append(" and phones like:phones").setParam("phones",
                    StringUtils.getLikewords(filter.getPhones()));
        }
        return query.listResult(filter);
    }

    /**
     * 保存到督查系统
     */
    public void savetask()
    {
        ResultSet set  = null;
        try {
            Connection conn  = SessionFactoryUtils.getDataSource(sessionFactory).getConnection();
            String call = "{call tasktest() }";
            CallableStatement cs = conn.prepareCall(call);
            cs.execute();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
